//https://www.cnblogs.com/shanfeng1000/p/16708878.html
import path from "path"
import fs from "fs"
// const path = require("path");
// const fs = require("fs");
try {
  const vue_bundler_file = path.resolve(
    __dirname,
    "node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.js"
  );
  //使用同步读取文件
  const data = fs.readFileSync(vue_bundler_file, "utf8");
  //如果未添加过
  if (data.indexOf("sharedContext.$pruneCacheEntry") < 0) {
    console.log("正在修改源码文件：", vue_bundler_file);
    //先找到__v_cache变量的位置
    let index = data.indexOf("__v_cache");
    if (index >= 0) {
      // 继续找下一个大括号 }
      index = data.indexOf("}", index);
      if (index >= 0) {
        //从下一个位置开始
        index += 1;
        //然后放一个可以释放的函数
        const remove = "sharedContext.$pruneCacheEntry = pruneCacheEntry"
          // "        sharedContext.$pruneCacheEntry = function(key) {\r\n" +
          // "            const cached = cache.get(key);\r\n" +
          // "            if (cached) {\r\n" +
          // "                if (cached.key == current?.key) {\r\n" +
          // "                    resetShapeFlag(current);\r\n" +
          // "                } else {\r\n" +
          // "                    unmount(cached);\r\n" +
          // "                }\r\n" +
          // "                cache.delete(key);\r\n" +
          // "                keys.delete(key);\r\n" +
          // "            }\r\n" +
          // "        }\r\n"
        //然后拼接
        const result =
          data.substring(0, index) +
          "\r\n" +
          remove +
          "\r\n" +
          data.substring(index);
        fs.writeFileSync(vue_bundler_file, result, "utf8");
        console.log("修改源码文件成功：", vue_bundler_file);
      }
    }
  }
} catch (er: any) {
  console.error(er.message);
}

import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueDevTools(),
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    },
  },
  server: {
    port: 5177,
    proxy: {
      '/api': {
        target: 'http://localhost:10796',
        rewrite: (path) => path.replace('/api', ''),
        changeOrigin: true
      },
      '/upload': {
        target: 'http://localhost:10796',
        // pathRewrite: {'^/api/' : '/'},
        changeOrigin: true
      }
    }
  }
})
